<template>
	<div style="overflow:auto;height:3.1rem">
		<div class="operateDiv">
			<p class="operateText"><sup>*</sup>获奖条件:</p>
			<el-radio v-model="awardObtainType" label="0">无</el-radio>
			<el-radio v-model="awardObtainType" label="1" :disabled="activityScenes == '994001'">设置</el-radio>
			<el-select v-model="value" placeholder="" class="setSelect" :disabled="iSelectAward">
				<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled">
				</el-option>
			</el-select>满
			<el-input @blur="checkNumer($event,6,2,0.01,'obtainMoney')" minlength=1 placeholder="请输入金额" v-model="obtainMoney" class="setInput" :disabled="iSelectAward" style="width:8%">
			</el-input>
			<span class="checkTxt" v-show="diffAwardObtain" v-text="diffText" style="right:52%"></span>
		</div>
		<div class="operateDiv">
			<p class="operateText"><sup>*</sup>领奖条件:</p>
			<el-radio v-model="awardClaimType" label="0">无</el-radio>
			<el-radio v-model="awardClaimType" label="1" :disabled="activityScenes == '994001'">设置</el-radio>
			<el-select v-model="value1" placeholder="" class="setSelect" :disabled="iSelectClaim">
				<el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
				</el-option>
			</el-select>
			<el-input @blur="checkNumer($event,8,0,1,'claimMoney')" minlength=1 placeholder="请输入积分" v-model="claimMoney" class="setInput" :disabled="iSelectClaim" style="width:8%">
			</el-input>
			<span class="checkTxt" v-show="diffAwardClaim" v-text="diffText" style="right:52%"></span>
		</div>
		<div class="operateDiv">
			<p class="operateText"><sup>*</sup>普通奖项:</p>
			<div class="butInfoMes edit" @click="addMoneyRule" v-show="repeatRecord == 0">新建奖项</div>
			<div class="butInfoMes edit" @click="addGift" v-show="repeatRecord == 0">新建礼品</div>
		</div>
		<div class="operateDiv" style="margin-left:-8%">
			<p class="operateText"></p>
			<Prize v-for="(obj,index) in commonPrize1" :key="index" :obj="obj" :pos="index*1+1" :type="1" @enlargeImg="enlargeImg" :leng="commonPrize.length" @detail="detail"></Prize>
		</div>
		<div class="operateDiv" style="margin-left:-8%">
			<p class="operateText"></p>
			<Prize v-for="(obj,index) in commonPrize2" :key="index" :obj="obj" :pos="index*1+5" :type="1" @enlargeImg="enlargeImg" :leng="commonPrize.length" @detail="detail"></Prize>
		</div>
		<!-- <div class="operateDiv redBagDiv">
			<p class="operateText">惊喜红包:</p>
			<el-checkbox v-model="awardSurprise">启用惊喜红包</el-checkbox>
			<p class="giveSet" @click="setGive" v-show="awardSurprise != 0">发放规则设置</p>
			<span class="checkTxt" v-show="diffAwardSurprise" v-text="diffText" style="right:65%"></span>
		</div> -->
		<div v-show="showGiveSetDiv">
			<GiveSet @closeGiveSet='closeGiveSet'></GiveSet>
		</div>
		<div v-if="showMoneyRuleDiv">
			<MoneyRule :pos="prizePos" @closeMoneyRule='closeMoneyRule' :prizeID="prizeID" :leng="commonPrize.length" :obj="itemObj" :giftID="giftID" :btnType="btnType" @showStationDialog="showStationDialog"></MoneyRule>
		</div>
		<div v-if="showAddGift">
			<AddGift :pos="prizePos" @closeAddGift='closeAddGift' :leng="commonPrize.length" :prizeID="prizeID" :firstID="firstID" :giftID="giftID" :btnType="btnType"></AddGift>
		</div>
	</div>
</template>

<script>
	import GiveSet from './GiveSet'
	import MoneyRule from './MoneyRule'
	import AddGift from './AddGift'
	import Prize from './Prize'
	import {FIND_LOTTERY_BY_CODE,
			ADD_RED_PACKAGE_OR_COUPONRULE,
			DELETE_RED_PACKAGE_OR_COUPONrULE_BY_ID,
			ADD_GIFTS,
			FIND_COUPON_RED_BY_CODE,
			} from '@/config/url'
	import Bus from '@/bus.js'
	import { publicVM } from "_config/public.js"

	export default {
		mixins:[publicVM],
		components: {
			GiveSet,
			MoneyRule,
			AddGift,
			Prize,
		},
		props:['prizeID','awardSort','lotterData'],
		data() {
			return {
				activityScenes:sessionStorage.getItem('activityScenes'),//场景
				giftID:null,
				itemObj:null,
				btnType:'3',
				value: '1',
				value1: '1',
				checked: '',
				isCharge:true,//场景是否是充电
				options1: [{
					value: '1', 
					label: '会员积分'
				}],
				showGiveSetDiv: false,
				showMoneyRuleDiv: false,
				showAddGift: false,
				prizePos:null,//奖品在列表中的位置
				activityCode:sessionStorage.getItem('activityCode'),//活动code
				awardObtain:'0',//获奖条件：0 不限，1金额，2电量
				awardObtainType:'0',
				obtainMoney:null,//满足获奖的金额
				awardClaim:'0',//获奖条件：0 不限，1 会员积分
				awardClaimType:'0',
				claimMoney:null,//可领取奖项的数值
				awardSurprise:'0',//惊喜红包：0 不开启，1 开启
				awardSurpriseType:'0',//惊喜红包类型：0 百分比中奖，1 老用户回归
				addsWinning:null,//获奖几率
				triggerData:null,//老用户触发时间
				triggerCondition:'0',//老用户触发条件：0 未充电
				repeatRecord:sessionStorage.getItem('repeatRecord'),//活动复录：0 初录，1 复录
				iSelectAward:true,//是否可以选择获奖条件
				iSelectClaim:true,//是否可以选择领奖条件
				lotteryContent:null,//阶梯内容
				diffText:'与初录不一致',
				diffAwardObtain:false,
				diffAwardClaim:false,
				diffAwardSurprise:false,
				firstPrizeList:null,
				firstCommonPrize:null,
				firstID:null,
				pos:null,
				up:null,
			}
		},
		created(){
			Bus.$off('addItem')
			Bus.$on('addItem',(data)=>{
				this.addComPrize(data)
			})
			Bus.$off('addPrize')
			Bus.$on('addPrize',()=>{
				this.getPrizeID()
			})

		},
		watch:{
			//获奖条件
			awardObtainType(val){
				if(val == 0){
					this.awardObtain = '0'
					this.obtainMoney = 0
					this.iSelectAward = true
				}else if(val == 1){
					this.iSelectAward = false
					this.awardObtain = this.value
					// if(this.repeatRecord == 0){
						this.obtainMoney = this.lotteryContent.obtainMoney?this.lotteryContent.obtainMoney:'0.01'
					// }
				}

				this.checkAwardObtain()
			},
			value(){
				this.checkAwardObtain()
			},
			obtainMoney(val){
				val = Number(val)
				// if(val == 0 || isNaN(val)){
				// 	this.$message({
				// 		message: '最多输入1~6位正整数',
				// 		type: 'warning'
				// 	})
				// }
				this.checkAwardObtain()
			},
			
			prizeID(val){
				this.prizeID = val
			},
			awardObtain:{
				handler:function(val){
					if(val == 0){
						this.awardObtainType = '0'
						this.iSelectAward = true
						this.obtainMoney = 0
					}else{
						this.awardObtainType = '1'
						if(this.repeatRecord == 0){
							this.obtainMoney = this.lotteryContent.obtainMoney?this.lotteryContent.obtainMoney:'0.01'
						}
						this.iSelectAward = false
					}
					if(this.repeatRecord == 1){
						if(this.repeatRecordContent){
							if(val != this.repeatRecordContent.awardObtain){
								this.diffAwardObtain = true
							}else{
								this.diffAwardObtain = false
								if(val > 0){
									if(this.obtainMoney == this.repeatRecordContent.obtainMoney){
										this.diffAwardObtain = false
									}else{
										this.diffAwardObtain = true
									}
								}
							}
						}
					}
				},
				immediate: true
			},
			//领奖条件
			awardClaimType(val){
				if(val == 0){
					this.awardClaim = '0'
					this.claimMoney = 0.01
					this.iSelectClaim = true
				}else if(val == 1){
					this.iSelectClaim = false
					this.awardClaim = this.value1
					if(this.repeatRecord == 0){
						this.claimMoney = this.lotteryContent.claimMoney?this.lotteryContent.claimMoney:'0.01'
					}
				}

				this.checkAwardClaim()
			},
			value1(){
				this.checkAwardClaim()
			},
			claimMoney(val){
				val = Number(val)
				// if(val == 0 || isNaN(val)){
				// 	this.$message({
				// 		message: '最多输入1~8位正整数',
				// 		type: 'warning'
				// 	})
				// }
				this.checkAwardClaim()
			},
			awardClaim:{
				handler:function(val){
					if(val == 0){
						this.awardClaimType = '0'
						this.iSelectClaim = true
						this.claimMoney = 0
					}else{
						this.awardClaimType = '1'
						this.iSelectClaim = false
						this.claimMoney = this.lotteryContent.claimMoney || 1
					}
	
					if(this.repeatRecord == 1 && this.lotteryContent){
						if(val != this.lotteryContent.awardClaim){
							this.diffAwardClaim = true
						}else{
							this.diffAwardClaim = false
							if(val > 0){
								if(this.value == this.lotteryContent.awardClaim){
									this.diffAwardClaim = false
								}else{
									this.diffAwardClaim = true
								}
							}
						}
					}
				},
				immediate: true
			},
			// awardSurprise(val){
			// 	val = val ? 1 : 0
			// 	if(this.repeatRecord == 1 && this.lotteryContent){
			// 		if(val != this.lotteryContent.awardSurprise){
			// 			this.diffAwardSurprise = true
			// 		}else{
			// 			this.diffAwardSurprise = false
			// 			// if(this.value == this.lotteryContent.awardClaim){
			// 			// 	this.diffAwardClaim = false
			// 			// }else{
			// 			// 	this.diffAwardClaim = true
			// 			// }
			// 		}
			// 	}
			// },
			lotterData:{
				handler:function(val){
					if(val){
						if(this.lotteryContent){
							this.lotteryContent = val[sessionStorage.getItem('lotterySort') - 1]
							this.awardObtain = this.lotteryContent.awardObtain || '0'
							this.obtainMoney = this.lotteryContent.obtainMoney || 0
							this.claimMoney = this.lotteryContent.claimMoney || 0
							this.awardClaim = this.lotteryContent.awardClaim || '0'
							// this.awardSurprise = this.lotteryContent.awardSurprise || '0'
						}
					}
					if(sessionStorage.getItem('activityScenes') == '994003'){
						this.isCharge = false
					}else{
						this.isCharge = true
					}
				},
				immediate: true
			},
		},
		computed:{
			options(){
				return [
					{
						value: '1',
						label: '金额'
					}, {
						value: '2',
						label: '电量',
						disabled:this.isCharge
					}
				]
			},
			commonPrize(){
				set:{
					let firstData = this.$store.getters.getFirstCouponData
					let secData = this.$store.getters.getSecCouponData
					if(secData || firstData){
						if(this.repeatRecord == 0){
							return firstData
						}else{
							if(secData){
								return secData
							}
						}
					}else{
						return []
					}
				}
			},
			secData(){
				return this.$store.getters.getSecCouponData
			},
			firstData(){
				return this.$store.getters.getFirstCouponData
			},
			secSecPon(){
				return this.$store.getters.getSecLotteryData
			},
			commonPrize1(){
				if(this.commonPrize && this.commonPrize.length > 0){
					return this.commonPrize.slice(0,6)
				}
			},
			commonPrize2(){
				if(this.commonPrize && this.commonPrize.length > 6){
					return this.commonPrize.slice(6,this.commonPrize.length)
				}
			},
			repeatRecordContent(){
				return this.$store.getters.getFirstLotteryData[sessionStorage.getItem('lotterySort')-1]
			}
		},
		methods: {
			hasPrize(){
				return this.commonPrize.length > 0
			},
			updataData(val){
				if(val){
					this.lotteryContent = val
					this.awardObtain = this.lotteryContent.awardObtain || '0'
					this.obtainMoney = this.lotteryContent.obtainMoney || 0
					this.claimMoney = this.lotteryContent.claimMoney || 0
					this.awardClaim = this.lotteryContent.awardClaim || '0'
					// this.awardSurprise = this.lotteryContent.awardSurprise || '0'
				}
				if(sessionStorage.getItem('activityScenes') == '994003'){
					this.isCharge = false
				}else{
					this.isCharge = true
				}
			},
			//检查获奖条件是否一致
			checkAwardObtain(){
				if(this.repeatRecord == 1){
					if(this.awardObtain != this.repeatRecordContent.awardObtain){
						this.diffAwardObtain = true
					}else{
						if(this.awardObtain > 0){
							if(this.obtainMoney != this.repeatRecordContent.obtainMoney){
								this.diffAwardObtain = true
							}else{
								this.diffAwardObtain = false
							}
						}else{
							this.diffAwardObtain = false
						}
					}
				}
			},
			//检查领奖条件是否一致
			checkAwardClaim(){
				if(this.repeatRecord == 1){
					if(this.awardClaim != this.repeatRecordContent.awardClaim){
						this.diffAwardClaim = true
					}else{
						if(this.awardClaim > 0){
							if(this.claimMoney != this.repeatRecordContent.claimMoney){
								this.diffAwardClaim = true
							}else{
								this.diffAwardClaim = false
							}
						}else{
							this.diffAwardClaim = false
						}
					}
				}
			},
			// checkNum(e,type){
			// 	let val = Number($(e.target).val())
			// 	if(val ==0 || isNaN(val) || /^\./.test($(e.target).val())){
			// 		if(type){
			// 			$(e.target).val(type)
			// 		}else{
			// 			$(e.target).val(1)
			// 		}
			// 	}else{
			// 		if(type){
			// 			$(e.target).val(val.toFixed(2))
			// 		}
			// 	}
			// },
			getIsIdentical(){
				return  !(this.diffAwardObtain || this.diffAwardClaim || this.diffAwardSurprise);
			},
			getLotteryID(){
				console.log('阶梯id:'+this.prizeID)
				return this.prizeID
			},
			getPrizeID(){
				if(this.firstData && this.$store.getters.getSecLotteryData){
					let id = this.$store.getters.getSecLotteryData[sessionStorage.getItem('lotterySort')-1].id
					if(this.firstData.length != this.secData.length){
						for(let i=0;i<this.firstData.length;i++){
							if(!this.secData[i]){
								if(this.firstData[i].gifsType == '992010' || this.firstData[i].gifsType == '992020'){//优惠券
									this.$post(ADD_RED_PACKAGE_OR_COUPONRULE,{
										activityCode:this.activityCode,
										lotteryId:id,
										couponSort:i+1,
										winningProbability:'10000',
										giveLimitType:'0',
										couponType:this.firstData[i].gifsType,
										repeatRecord:this.repeatRecord,
										discountsPattern:this.firstData[i].discountsPattern
									})
										.then(res=>{
											if(res.status == 1){
												
											}
										})
								}else{
									this.$post(ADD_GIFTS,{
										activityCode:this.activityCode,
										lotteryId:id,
										giftsSort:i+1,
										winningProbability:'10000',
										giveLimitType:'0',
										prizePutNumber:'0',
										giveFrequencyLimit:0,
										couponType:this.firstData[i].gifsType,
										repeatRecord:this.repeatRecord,
										discountsPattern:null
									})
										.then(res=>{
											if(res.status == 1){
												
											}
										})
								}
							}
						}
						Bus.$emit('refreshList')
					}
				}
			},
			showStationDialog(){
				this.$emit('showStationDialog')
			},
			updataComPrize(){
				this.$fetch(FIND_COUPON_RED_BY_CODE,{
					'activityCode':this.activityCode,
					'repeatRecord':this.repeatRecord,
					'awardSort':this.prizeID
				})
					.then(res=>{
						if(res.status == 1){
							
						}else{
							this.$message({
								message: res.data,
								type: 'warning'
							});
						}
					})
					.catch(err=>{
						console.log(err)
						this.$message({
							message: '获取失败',
							type: 'warning'
						});
					})
			},
			addComPrize(data){
				this.itemObj = data.param
				this.commonPrize.push(data.param)
			},
			saveItem(pos){
				if(this.awardObtain == 0){
					this.obtainMoney == 0
				}

				if(this.awardClaim == 0){
					this.claimMoney == 0
				}
				
				let item = {}
				item.activityCode = this.activityCode
				item.awardObtain = this.awardObtain
				item.obtainMoney = this.obtainMoney
				item.awardClaim = this.awardClaim
				item.claimMoney = this.claimMoney
				item.awardSurprise = this.awardSurprise
				item.awardSurpriseType = this.awardSurpriseType
				item.addsWinning = this.addsWinning
				item.triggerData = this.triggerData
				item.repeatRecord = sessionStorage.getItem('repeatRecord')
				item.awardSort = this.awardSort
				if(this.repeatRecord == 0){
					item.id = this.$store.getters.getFirstLotteryData[pos].id
				}else{
					item.id = this.$store.getters.getSecLotteryData[pos].id
				}
				item.repeatRecord = this.repeatRecord
				item.isIdentical = this.repeatRecord == 0?0:this.getIsIdentical()?0:1
				this.$emit('sendItem',item)
			},
			setGive() {
				this.showGiveSetDiv = this.awardSurprise != 0;
			},
			closeGiveSet(data) {
				if(data){
					this.addsWinning = data.addsWinning
					this.triggerData = data.triggerData
					this.triggerCondition = data.triggerCondition
				}
				this.showGiveSetDiv = false;
			},
			addMoneyRule() {
				this.btnType = '3'
				this.showMoneyRuleDiv = true;
			},
			closeMoneyRule() {
				this.showMoneyRuleDiv = false;
			},
			enlargeImg(data) {
				this.$emit('enlargeImg', data)
			},
			addGift() {
				this.btnType = '3'
				this.showAddGift = true;
			},
			closeAddGift() {
				this.showAddGift = false;
			},
			detail(data){
				this.prizePos = data.pos
				this.itemObj = data.item
				this.btnType = data.type
				if(data.item.gifsType != '992030'){ 
					this.showMoneyRuleDiv = true
				}else{
					this.showAddGift = true
				}
				this.giftID = data.dataID 
				this.firstID = data.firstID 
			},
		},
		mounted(){
			console.log('1')
		},
		beforeDestroy(){
			Bus.$off('addItem')
			Bus.$off('addPrize')
		}
	}
</script>

<style>
	.operateDiv {
		width: 100%;
		margin: auto;
		margin-bottom: 0.08rem;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-left: 0;
		position: relative;
	}
	.checkTxt{
		color: #FF2E2E;
		position: absolute;
		right: -7%;
	}
	
	.operateText {
		text-align: end;
		width: 13%;
		margin-right: 0.08rem;
	}
	
	.setSelect {
		margin-right: 0.08rem;
		width: 0.7rem;
	}
	.setInput {
		margin-left: 0.08rem;
		width: 0.7rem;
	}
	
	.giveSet {
		color: #3395FF;
		text-decoration: underline;
		cursor: pointer;
	}
	
	.redBagDiv .el-checkbox {
		padding-top: 0.03rem;
	}
</style>